package com.wolfword.common.util.password;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import com.wolfword.common.util.number.NumberSystemUtil;

/**
 * 128位密钥的AES加解密
 * 
 * @author ServerZhang
 * @date 2015年7月9日
 */
public class AES_128Util {

	/**
	 * AES加密：密钥长度128位
	 * 
	 * @param str
	 * @param password
	 * @return
	 * @author ServerZhang
	 * @date 2015年7月9日
	 */
	public static String encrypt(String str, String password) {
		String newStr = "";
		try {
			KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
			keyGenerator.init(128, new SecureRandom(password.getBytes()));
			SecretKey secretKey = keyGenerator.generateKey();
			byte[] encoded = secretKey.getEncoded();
			SecretKeySpec keySpec = new SecretKeySpec(encoded, "AES");
			Cipher cipher = Cipher.getInstance("AES");// 创建密码器
			byte[] strBytes = str.getBytes("utf-8");
			cipher.init(Cipher.ENCRYPT_MODE, keySpec);// 初始化
			byte[] newBytes = cipher.doFinal(strBytes);// 加密
			newStr = NumberSystemUtil.bytes2HexStr(newBytes);//转换成十六进制
		} catch (NoSuchAlgorithmException e) {
			// KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
			// 没有这种算法异常
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// 创建密码器时
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// byte[] strBytes = str.getBytes("utf-8");
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			// 初始化时
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// 加密时
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// 加密时
			e.printStackTrace();
		}
		return newStr;
	}
	
	/**
	 * AES解密：密钥长度128位
	 * @param hexStr
	 * @param password
	 * @return
	 * @author ServerZhang
	 * @date 2015年8月10日
	 */
	public static String decrypt(String hexStr, String password){
		String newStr = "";
		//首先处理加密后的字符串为字节数组
		byte[] hexStr2Bytes = NumberSystemUtil.hexStr2Bytes(hexStr);
		try {
			KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
			keyGenerator.init(128, new SecureRandom(password.getBytes()));
			SecretKey secretKey = keyGenerator.generateKey();
			byte[] encoded = secretKey.getEncoded();
			SecretKeySpec secretKeySpec = new SecretKeySpec(encoded, "AES");
			Cipher cipher = Cipher.getInstance("AES");//创建密码器
			cipher.init(Cipher.DECRYPT_MODE, secretKey);//初始化
			byte[] finalByte = cipher.doFinal(hexStr2Bytes);
			newStr = new String(finalByte);
		} catch (NoSuchAlgorithmException e) {
			// KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
			// 没有这种算法异常
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// 创建密码器时抛出异常
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			// 初始化时抛异常
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// 加密时抛异常
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// 加密时抛异常
			e.printStackTrace();
		}
		return newStr;
	}

}
